/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package futbol.tacticas.concursantes.Potrosa;

import java.awt.Point;

/**
 *
 * @author IGadea
 */
public class Utilidades {

    private Utilidades() {
    }

    public static double distanciaPuntos(Point a, Point b) {
        return distanciaPuntos((double)a.x, (double)a.y, (double)b.x, (double)b.y);
    }

    public static double distanciaPuntos2(Point a, Point b) {
        return distanciaPuntos2((double)a.x, (double)a.y, (double)b.x, (double)b.y);
    }

    public static double distanciaPuntos(double ax, double ay, double bx, double by) {
        return Math.sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay));
    }

    public static double distanciaPuntos2(double ax, double ay, double bx, double by) {
        return (bx - ax) * (bx - ax) + (by - ay) * (by - ay);
    }

    public static double distanciaPuntoSegmento(double ax, double ay, 
                                                double bx, double by,
                                                double px, double py) {
        double r, dx, dy;
        if ((Math.abs(ax - bx) < 1.0) && (Math.abs(ay - by) < 1.0))
            return distanciaPuntos(ax, ay, px, py);
        dx = bx - ax;
        dy = by - ay;
        r = ((px - ax) * dx + (py - ay) * dy) / (dx*dx + dy*dy);
        if (r < 0)
            return 1000;//distanciaPuntos(ax, ay, px, py);
        else if (r > 1)
            return distanciaPuntos(bx, by, px, py);
        else
            return distanciaPuntos((1-r)*ax + r*bx, (1-r)*ay + r*by, px, py);
    }
    
    public static int control(int var, int a, int b) {
        if (var < a) return a;
        if (var > b) return b;
        return var;
    }
}
